package com.spark.check.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.spark.check.R
import com.spark.check.model.TaskModel

class ItemTaskView(context: Context, attrs: AttributeSet?, defStyle: Int) : LinearLayout(context, attrs, defStyle) {
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null, 0)

    override fun getRootView(): View {
        return LayoutInflater.from(context).inflate(R.layout.item_task, this)
    }
}

class ItemTaskContentView(context: Context, attrs: AttributeSet?, defStyle: Int) : LinearLayout(context, attrs, defStyle) {
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null, 0)

    init {
        orientation = VERTICAL
    }

    fun bind(datas: MutableList<TaskModel.Content>) {
        datas.forEach {
            val child = ItemTaskContentChildView(context)
            child.bind(it.title, it.content)
            addView(child)
        }
    }

}

class ItemTaskContentChildView(context: Context, attrs: AttributeSet?, defStyle: Int) : LinearLayout(context, attrs, defStyle) {
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null, 0)

    private var tv_name: TextView = TextView(context)
    private var tv_content: TextView = TextView(context)

    init {
        orientation = VERTICAL
        tv_name.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        tv_content.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)

        tv_name.setTextColor(resources.getColor(R.color.color_bf))
        tv_name.textSize = 13F

        tv_content.setTextColor(resources.getColor(R.color.color333))
        tv_content.textSize = 13F

        addView(tv_name)
        addView(tv_content)
    }

    fun bind(name: String = "", content: String = "") {
        tv_name.text = name
        tv_content.text = content
    }
}